<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form inline size="mini">
        <el-row class="bg-row">
          <el-col :span="24">
            <el-form-item label="学校区域">
              <el-form-item label-width="0">
                <el-select v-model="dataForm.provinceCode" filterable default-first-option clearable @change="getCityOptions">
                  <el-option v-for="item in provinceOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label-width="0">
                <el-select v-model="dataForm.cityCode" filterable default-first-option clearable @change="getAreaOptions">
                  <el-option v-for="item in cityOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label-width="0">
                <el-select v-model="dataForm.areaCode" filterable default-first-option clearable>
                  <el-option v-for="item in areaOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row">
          <el-col :span="24">
            <el-form-item label="学年">
                <el-col :span="11">
                  <el-date-picker v-model="dataForm.fromYear" type="year" value-format="yyyy" placeholder="选择年"></el-date-picker>
                </el-col>
                <el-col :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker v-model="dataForm.toYear" type="year" value-format="yyyy" placeholder="选择年"></el-date-picker>
                </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row">
          <el-col :span="24">
            <el-form-item label="学校类型">
              <el-select v-model="dataForm.schoolType" @change="schoolTypeChangeHandle" clearable>
                <el-option v-for="item in typeOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学校班级">
              <el-form-item label-width="0">
                <el-select filterable default-first-option clearable v-model="dataForm.schoolId" @change="getGradeOption">
                  <el-option v-for="item in schoolOptions" :value="item.id" :label="item.schoolName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label-width="0">
                <el-select filterable default-first-option clearable v-model="dataForm.gradeId" @change="getClassOption">
                  <el-option v-for="item in gradeOptions" :value="item.id" :label="item.gradeName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label-width="0">
                <el-select filterable default-first-option clearable v-model="dataForm.classId">
                  <el-option v-for="item in classOptions" :value="item.id" :label="item.className" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row">
          <el-col :span="6">
            <el-form-item label="学生学号">
              <el-input v-model="dataForm.sno"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="学生名称">
              <el-input v-model="dataForm.studentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="dataForm.status">
                <el-option v-for="item in statusOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row">
        <el-col :span="8">
          <el-form-item label="地址" prop="address">
            <el-radio-group v-model="dataForm.address">
              <el-radio :label="1">金华眼科医院</el-radio>
              <el-radio :label="2">江南近规防控</el-radio>
              <el-radio :label="3">武义分院</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">

            <el-link :href="printUrl" target="_blank" type="primary" :disabled="isDisabled">
              <el-button size="mini" type="primary" @click="getALetterToParents">查 询</el-button>
            </el-link>
          </el-form-item>
        </el-row>
      </el-form>
      <search-stock v-if="stockVisible" ref="stockInfo" @refreshData="getStockInfoList"></search-stock>
      <!-- 弹窗，查询制造商 -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
      <!-- 弹窗，查询商品品种 -->
      <select-brand v-if="brandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
    </basic-container>
  </div>
</template>

<script>
let params = "";
 import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { remote } from '@/api/admin/dict'
  import { getListByParentId } from '@/api/admin/region'
  import { getCurrentYear } from '@/api/pvm/filingRate'
  import { fetchList, delObj,printStudent,printOneStudent,syncStudent, studentTransfer, studentSuspension } from '@/api/pvm/student'
  import { getSchoolList , getClassOptions, getSchoolListBySchoolType } from '@/api/pvm/school'
  import { getGradeBySchoolId } from '@/api/pvm/grade'



export default {
  data() {
    return {
      printUrl: "",
      isDisabled: true,
      dataForm: {
        address:1
      },
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      typeOptions: [],
      schoolOptions: [],
      gradeOptions: [],
      classOptions: [],
      statusOptions: [],
      supplyVisible: false,
      brandVisible: false,
      stockVisible: false
    };
  },
  computed: {
    ...mapGetters(["userInfo", "bizData"])
  },
  components: {
  },
  created() {
    this.getProvinceOptions()
    this.getSchoolTypeList()
    this.getSchoolOptions()
    this.getStudentStatus()
    this.getCurrentYear()
  },
  methods: {
    // 获取当前学年
    getCurrentYear(){
      getCurrentYear().then(response => {
        let resultData = response.data.data
        let yearArr = resultData.split(',')

        this.$set(this.dataForm,'fromYear',yearArr[0])
        this.$set(this.dataForm,'toYear',yearArr[1])
      })
    },
    // 获取省级地址列表
      getProvinceOptions(){
        var parentId = '86';
        getListByParentId(parentId).then(response => {
          this.provinceOptions = response.data.data;
        }).catch(() => {
          alert("省级地址出错了，请联系管理员a ")
        });
      },
      // 获取市级地址列表
      getCityOptions(val){
        this.cityOptions = []
        this.areaOptions = []
        this.$set(this.dataForm,'cityCode','')
        this.$set(this.dataForm,'areaCode','')
        if(val){
          getListByParentId(val).then(response => {
            this.cityOptions = response.data.data;
          }).catch(() => {
            alert("市级地址出错了，请联系管理员a ")
          });
        }
      },
      // 获取区/县级地址列表
      getAreaOptions(val){
        this.areaOptions = []
        this.$set(this.dataForm,'areaCode','')
        if(val){
          getListByParentId(val).then(response => {
            this.areaOptions = response.data.data;
          }).catch(() => {
            alert("市级地址出错了，请联系管理员a ")
          });
        }
      },
      // 获取学校类型列表
      getSchoolTypeList(){
        remote('school_type').then(result => {
          let resultData = result.data.data
          let list = []
          resultData.forEach(item => {
            list.push({label: item.label, value: item.value*1})
          })
          this.typeOptions = list
        })
      },
      schoolTypeChangeHandle(val){
        this.getSchoolOptions()
      },
      // 获取学校列表
      getSchoolOptions(data){
         let schoolType = ''
        if(this.dataForm.schoolType === 0){
          schoolType = 0
        }else{
          schoolType = this.dataForm.schoolType || ''
        }
        getSchoolListBySchoolType(schoolType).then(result => {
          this.schoolOptions = result.data.data
        })
      },
    // 获取年纪列表
      getGradeOption(schoolId){
        this.gradeOptions = []
        this.classOptions = []
        this.$set(this.dataForm,'gradeId','')
        this.$set(this.dataForm,'classId','')
        getGradeBySchoolId(schoolId).then(result => {
          this.gradeOptions = result.data.data
        })
      },
      // 获取班级列表
      getClassOption(gradeId){
        this.classOptions = []
        this.$set(this.dataForm,'classId','')
        getClassOptions(this.dataForm.schoolId,gradeId).then(result => {
          this.classOptions = result.data.data
        })
      },
      // 获取学生状态列表
      getStudentStatus(){
        remote('student_status').then(result => {
          let resultData = result.data.data
          let list = []
          resultData.forEach(item => {
            list.push({label: item.label, value: item.value*1})
          })
          this.statusOptions = list
        })
      },
      //  转换类型格式
      statusFormat(val){
        let obj = {}
        obj = this.typeOptions.find(item => {
          if(item.value === val){
            return item
          }
        })
        if(obj && obj.label){
          return obj.label
        }
      },
    getALetterToParents() {
      if(!this.dataForm.fromYear){
        this.$message.warning('请输入学年')
        return
      }
      if(!this.dataForm.toYear){
        this.$message.warning('请输入学年')
        return
      }
      this.isDisabled = false
      if(this.dataForm.address ==1){
        params = "/a_letter_to_parents1.cpt";
      }else if(this.dataForm.address ==2){
        params = "/a_letter_to_parents2.cpt";
      }else if(this.dataForm.address ==3){
        params = "/a_letter_to_parents3.cpt";
      }
      let data = filterParams(this.dataForm);
      let parameter = "";
      for (let key in data) {
        parameter += "&" + key + "=" + this.dataForm[key];
      }
      this.printUrl = this.bizData.printUrl + params + parameter;

    }
  }
};
</script>

<style lang="scss" scoped>
.row-bg {
  background-color: #cadee8;
  // padding-left: 15px;
  padding-right: 15px;
  padding-top: 5px;
  margin-bottom: 2px;
}
</style>
